using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DocumentBuilder.Controls
{
    public class DocumentControl : IControl
    {
        public bool EarlyEvaluate { get; private set; }
        public String Path { get; private set; }
        public Document Document { get; private set; }

        private String _containerDivTemplate = "<div style='width:100%; height:100%; display:-webkit-inline-box; -webkit-box-align: center; -webkit-box-pack: center;' id={0}><img src='http://localhost:52646/DocumentBasedSite/Images/loading.gif'></img></div>";

        private DocumentControl(String path, bool earlyEvaluate)
        {
            Path = path;
            EarlyEvaluate = earlyEvaluate;
        }

        public static DocumentControl Create(Document context, XmlNode documentNode)
        {
            string path = String.Empty;
            if (documentNode.Attributes != null && documentNode.Attributes["path"] != null)
                path = documentNode.Attributes["path"].Value;
            else
                throw new Exception("Document could not added without path");

            bool earlyEvaluate = false;
            if (documentNode.Attributes != null && documentNode.Attributes["earlyevaluate"] != null)
                earlyEvaluate = bool.Parse(documentNode.Attributes["earlyevaluate"].Value);

            var documentControl = new DocumentControl(path, earlyEvaluate);

            if (earlyEvaluate)
            {

                var document = new Document();
                document.Load(path, context.LoadContext);

                context.View.Scripts.AddRange(document.View.Scripts);
                context.View.Css.AddRange(document.View.Css);

                foreach (var dependentDocument in document.DependentDocuments)
                {
                    if (context.DependentDocuments.ContainsKey(path))
                        context.DependentDocuments[path].Add(context.LoadContext);
                    else
                    {
                        context.DependentDocuments.Add(path, new List<DocumentLoadContext> { context.LoadContext });
                    }
                }

                documentControl.Document = document;
            }
            else
            {
                var documentLoadContext = new DocumentLoadContext();

                if (context.LoadContext != null)
                    documentLoadContext.Attributes = context.LoadContext.Attributes;

                documentLoadContext.ContainerId = path.Replace(".", "_").Replace("/", "_");

                if (context.DependentDocuments.ContainsKey(path))
                    context.DependentDocuments[path].Add(documentLoadContext);
                else
                {
                    context.DependentDocuments.Add(path, new List<DocumentLoadContext> { documentLoadContext });
                }
            }

            return documentControl;
        }

        #region IControl Members

        public void PaintHtmlString(StringBuilder htmlCanvas)
        {
            if (EarlyEvaluate)
            {
                htmlCanvas.AppendLine(Document.View.Html);
            }
            else
            {
                htmlCanvas.AppendLine(String.Format(_containerDivTemplate, Path.Replace(".", "_").Replace("/", "_")));
            }
        }

        #endregion
    }
}
